<?php

// +----------------------------------------------------------------------
// | 后台登录页面
// +----------------------------------------------------------------------

namespace Admins\Controller;

use Common\Controller\AdminBase;
use Admins\Lib\Admin;

class LoginController extends AdminBase{

    //后台登陆界面
    public function index() {
        //如果已经登录
        if (Admin::getInstance()->id) {
            $this->redirect('/Admins');
        }
        $this->display();
    }


    //后台登陆验证
    public function login() {
        //记录登陆失败者IP
        $ip = get_client_ip();
        $username = I("post.account", "", "trim");
        $password = I("post.password", "", "trim");
        if (empty($username)) {
            $this->error("用户名不能为空，请重新输入！");
        }
        if(empty($password)){
            $this->error("密码不能为空，请重新输入！");
        }

        if (Admin::getInstance()->login($username, $password)) {
            $forward = cookie("forward");
            if (!$forward) {
                $forward = U("Admins:Index");
            } else {
                cookie("forward", NULL);
            }
            //增加登陆成功行为调用
            $admin_public_tologin = array(
                'username' => $username,
                'ip' => $ip,
            );
            tag('admin_public_tologin', $admin_public_tologin);
            $this->success('成功登录后台管理系统',U('Index/index'));
        } else {
            //增加登陆失败行为调用
            $admin_public_tologin = array(
                'username' => $username,
                'password' => $password,
                'ip' => $ip,
            );
            tag('admin_public_tologin_error', $admin_public_tologin);
            $this->error("用户名或者密码错误，登陆失败！", U("login"));
        }
    }

    //退出登陆
    public function logout() {
        if (Admin::getInstance()->logout()) {
            //手动登出时，清空forward
            cookie("forward", NULL);
            $this->success('注销成功！', U("index"));
        }
    }

}